package com.joinlabs.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.joinlabs.entity.CarProduct;
import com.joinlabs.entity.Shopcar;
import com.joinlabs.entity.ShopcarItem;
import com.joinlabs.mapper.ShopcarMapper;
import com.joinlabs.service.IShopcarService;

@Service
public class ShopcarServiceImpl implements IShopcarService {
	
	@Resource
	private ShopcarMapper shopcarMapper;


	@Override
	public int delShopcarItemByCarId(BigDecimal carId) {
		//删除购物车子项
		int row = shopcarMapper.deleteByPrimaryKey(carId);
		
		return row;
	}

	@Override
	public int createShopcar(ShopcarItem shopcarItem) {
		//生成购物车子项
		int row = shopcarMapper.insert(shopcarItem);
		
		return row;
	}

	@Override
	public Shopcar findShopcarItemsByUserId(String userId) {
		Shopcar shopcar = new Shopcar();
		//设置用户Id
		shopcar.setUserId(userId);
		//通过用户Id查询购物车并设置
		shopcar.setItems(shopcarMapper.findShopcarItemsByUserId(userId));
		
		return shopcar;
	}

	@Override
	public void plusCountByCarId(BigDecimal carId) {
		//进行商品数量的增加
		shopcarMapper.plusCountByCarId(carId);
		
	}

	@Override
	public void minusCountByCarId(BigDecimal carId) {
		//进行商品数量的减少
		shopcarMapper.minusCountByCarId(carId);
		
	}

	@Override
	public List<ShopcarItem> findShopcarItemsByIds(BigDecimal[] carIds) {
		//通过购物车子项Id集合  查询购物车子项集合(订单操作)
		List<ShopcarItem> list = shopcarMapper.findShopcarItemsByCid(carIds);
		
		return list;
	}

	@Override
	public void delShopcarItemsByIds(BigDecimal[] carIds) {
		//通过购物车子项id  删除下完单的购物车子项(订单操作)
		shopcarMapper.delShopcarItemsByIds(carIds);
		
	}

	@Override
	public void delWaresCountById(List<ShopcarItem> list) {
		//通过购物车id  进行库存减少(下单操作)
		for(int i = 0;i<list.size();i++){
			//1.取到每一次遍历出的商品Id
			BigDecimal waresId = list.get(i).getWaresId();
			//2.取到每一次遍历出的商品购买数量并转换成Long型
			Long waresCount = list.get(i).getWaresCount().longValue();
			//3.减库存并加销量
			shopcarMapper.delWaresCountById(waresId, waresCount);
		}
		
	}

	@Override
	public List<CarProduct> findRecommendWares() {
		//查询推荐商品
		List<CarProduct> list = shopcarMapper.findRecommendWares();
		
		return list;
	}
	
	
}
